package cn.edu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 修改密码
 */
public class JdbcDemo05 {
    static String url = "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    static String user = "root";
    static String password = "root";

    public static void main(String[] args) {
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
            System.out.println("与数据库建立好连接了~~");

            //接收用户输入的内容
            Scanner sc = new Scanner(System.in);
            System.out.println("开始修改密码了~~~");
            System.out.println("请输入用户名：");
            String username = sc.nextLine();
            System.out.println("请输入密码：");
            String password = sc.nextLine();

            Statement st = conn.createStatement();
            String sql = "update mydb01 set password = '" + password + "' where username = '" + username + "'";
            int rows = st.executeUpdate(sql);
            if (rows > 0){
                System.out.println("修改密码成功~~");
            } else{
                System.out.println("修改密码失败~~~");
            }
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
